#include "event_demo.h"

EVENT_CB_S g_pevent1;
EVENT_CB_S g_pevent2;
extern EVENT_CB_S g_pevent;
UINT32 g_testCount1;
UINT32 g_testCount2;
UINT32 g_EventTest_TaskID1;
UINT32 g_EventTest_TaskID2;

static void TaskF01()
{
    UINT32 ret;
    g_testCount1++;
    printf("\n++++++Yang Zihao's TaskF01, before event(AND) read, the g_testCount1 = %d. \n", g_testCount1);
    ret = LOS_EventRead(&g_pevent1, 0x11, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);
    g_testCount1++;
    printf("\n++++++Yang Zihao's TaskF01:after event(AND) read, the g_testCount1 = %d. \n", g_testCount1);
    
    LOS_EventClear(&g_pevent1, 0x11);
    printf("\n++++++Yang Zihao's TaskF01, before event(OR) read, the g_testCount1 = %d. \n", g_testCount1);
    ret = LOS_EventRead(&g_pevent1, 0x11, LOS_WAITMODE_OR, LOS_WAIT_FOREVER);
    g_testCount1++;
    printf("\n++++++Yang Zihao's TaskF01:after event(OR) read, the g_testCount1 = %d. \n", g_testCount1);
EXIT:
    ret = LOS_TaskDelete(g_EventTest_TaskID1);
    return;
}



static VOID TaskF02()
{
    UINT32 ret;
        printf("\n++++++Yang Zihao's TaskF02, before event read, the g_testCount2 = %d. \n", g_testCount2);
        printf("\n++++++Yang Zihao's TaskF02: reading the event(0x11)...wait for 100 time cycles... \n");
        ret = LOS_EventRead(&g_pevent2, 0x11, LOS_WAITMODE_AND, 100);   

        printf("\n++++++Yang Zihao's TaskF02: has read the event, going to do the g_testCount2++... \n");
        g_testCount2++;
        printf("\n++++++Yang Zihao's TaskF02: after the event reading, the g_testCount2 is %d. \n", g_testCount2);
        printf("\n++++++Yang Zihao's TaskF02: waiting to read the event(0x4)...wait forever...\n");
        ret = LOS_EventRead(&g_pevent2, 0x4, LOS_WAITMODE_AND, LOS_WAIT_FOREVER);

        g_testCount2++;
        printf("\n++++++Yang Zihao's TaskF02: after the event reading(0x4), the g_testCount2 is %d. \n", g_testCount2);

    EXIT:
        LOS_TaskDelete(g_EventTest_TaskID2);
        printf("\n++++++Yang Zihao's TaskF02:deleted!\n");  
}



void event_demo()
{   
   UINT32 ret;
   printf("\n++++++Yang Zihao's EventTest001: event CB1 init begins.\n");
   g_pevent1.uwEventID = 0x0;
   ret = LOS_EventInit(&g_pevent1);
   if(ret == LOS_OK)
   {
        printf("\n++++++Yang Zihao's EventTest001: event CB1 init finished.\n");
   }

   TSK_INIT_PARAM_S task1;
   (void)memset_s(&task1, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
   task1.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF01;
   task1.pcName = "EventTsk1";
   task1.uwStackSize = TASK_STACK_SIZE_TEST;
   task1.usTaskPrio = TASK_PRIO_TEST - 2;
   task1.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_EventTest_TaskID1, &task1);
    if(ret == LOS_OK)
        printf("\n++++++Yang Zihao's TaskF01 Task: Created.\n");
    else
        return;

    g_testCount1 = 0;
    printf("\n++++++Yang Zihao's EventTest001: write event(AND) 0x10...\n");
    ret = LOS_EventWrite(&g_pevent1, 0x10);
    LOS_TaskDelay(10);

    printf("\n++++++Yang Zihao's EventTest001: write event(AND) 0x10...\n");
    ret = LOS_EventWrite(&g_pevent1, 0x01);
    LOS_TaskDelay(10);

    printf("\n++++++Yang Zihao's EventTest001: write event(OR) 0x10...\n");
    ret = LOS_EventWrite(&g_pevent1, 0x01);
    LOS_TaskDelay(10);

    ret = LOS_EventDestroy(&g_pevent1);
}
    
void event_demo1()
{   
    UINT32 ret;
    g_testCount2 = 0;
    printf("\n++++++Yang Zihao's EventTest002: event CB2 init begins.\n");  
    g_pevent2.uwEventID = 0x0;
    LOS_EventInit(&g_pevent2);
    printf("\n++++++Yang Zihao's EventTest002: event CB2 init finished.\n");

    TSK_INIT_PARAM_S task2;
    (void)memset_s(&task2, sizeof(TSK_INIT_PARAM_S), 0, sizeof(TSK_INIT_PARAM_S));
    task2.pfnTaskEntry = (TSK_ENTRY_FUNC)TaskF02;
    task2.pcName = "EventTsk2";
    task2.uwStackSize = TASK_STACK_SIZE_TEST;
    task2.usTaskPrio = TASK_PRIO_TEST - 2;
    task2.uwResved = LOS_TASK_STATUS_DETACHED;

    ret = LOS_TaskCreate(&g_EventTest_TaskID2, &task2);
    if(ret == LOS_OK)
        printf("\n++++++Yang Zihao's TaskF02 Task: Created.\n");
    else
        return;
    
    LOS_TaskDelay(10);

    LOS_EventWrite(&g_pevent2, 0x2);
    printf("\n++++++Yang Zihao's EventTest002, write (0x2) to event, the current uwEventID = %u.\n", g_pevent2.uwEventID);

    LOS_TaskDelay(10);

    LOS_EventWrite(&g_pevent2, 0x11);
    printf("\n++++++Yang Zihao's EventTest002, write (0x11) to event, the current uwEventID = %u.\n", g_pevent2.uwEventID);

    LOS_TaskDelay(10);

    LOS_EventClear(&g_pevent2, 0xffff);
    printf("\n++++++Yang Zihao's EventTest002, clear all events with 0xffff, the current uwEventID = %u.\n", g_pevent2.uwEventID);

    ret = LOS_EventDestroy(&g_pevent2);
    if(ret == LOS_OK)
        printf("\n++++++Yang Zihao's EventTest_TaskID2: event deleted.\n");
    ret = LOS_TaskDelete(g_EventTest_TaskID2);
    if(ret == LOS_OK)
        printf("\n++++++Yang Zihao's EventTest_TaskID2: deleted.\n");
    
    return;    
}
